﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DLV.Data.Entities;
using DLV.FrameWork;
using DLV.Library.Utilities;

namespace DLV.BackEnd.Pages
{
    public partial class ChietTinhHuongDanVien : AdminPageBase
    {
        protected List<CalculatorInfo> TravelGuideCashs = new List<CalculatorInfo>();
        protected decimal TotalTravelGuidePrices = 0;
    


        private List<CalculatorInfo> _calculatorList = new List<CalculatorInfo>();
        private readonly CalculatorController _calculatorController = new CalculatorController();
        protected TourInfo tourInfo = new TourInfo();
        private TourInfo _tourInfo = new TourInfo();
        private  readonly TourController _tourController = new TourController();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(Request.QueryString["TourId"]))
            {
                _calculatorList = _calculatorController.ListCalculator().Where(p=>p.TourId==int.Parse(Request.QueryString["TourId"])).ToList();
                tourInfo = new TourController().GetTour(int.Parse(Request.QueryString["TourId"]));
                //_tourInfo = tourInfo;
                LoadLevel();
                LoadGender();
                LoadCity();
                LoadTime();
                LoadTravelGuide();

                RenderForm();
            }
            //if (_calculatorList.Count == 0) return;
            
            
        }
        private void RenderForm()
        {
            TravelGuideCashs = _calculatorList.Where(p => p.ItemType == 9).ToList();
            TotalTravelGuidePrices = TravelGuideCashs.Sum(p=>p.TotalPriceTravelGuide);
        }
        protected void lbtSave_Click(object sender, EventArgs e)
        {
            SaveData();
            //RedirectLink();
        }
        protected void lbtClose_Click(object sender, EventArgs e)
        {
            //RedirectLink();
        }
        private void SaveData()
        {
            _tourInfo.QuantityAdult = int.Parse(txtQuantityAdultsTour.Text);
            _tourInfo.QuantityChild = int.Parse(txtQuantityChildTour.Text);
            if (!String.IsNullOrEmpty(dteBeginDateTour.Value))
            {
                var beginDate = Convert.ToDateTime(dteBeginDateTour.Value);
                _tourInfo.BeginDate = new DateTime(beginDate.Year, beginDate.Month, beginDate.Day, int.Parse(ddlBeginHourTour.SelectedValue), int.Parse(ddlBeginMinuteTour.SelectedValue), 0);
            }
            if (!String.IsNullOrEmpty(dteEndDateTour.Value))
            {
                var endDate = Convert.ToDateTime(dteEndDateTour.Value);
                _tourInfo.EndDate = new DateTime(endDate.Year, endDate.Month, endDate.Day, int.Parse(ddlEndHourTour.SelectedValue), int.Parse(ddlEndMinuteTour.SelectedValue), 0);
            }
            _tourInfo.QuantityDay = int.Parse(txtTotalDayTour.Text);
            _tourInfo.QuantityNight = int.Parse(txtTotalNightTour.Text);
            _tourInfo.UpdatedBy = UserId;
            _tourInfo.UpdatedDate = DateTime.Now;
            _tourController.UpdateTour(_tourInfo);
            
            
        }
        private void LoadTime()
        {
            for (var i = 0; i < 24; i++)
            {
                //var hour = i < 10 ? "0" + i : i.ToString();
                ddlBeginHourTour.Items.Add(new ListItem(i.ToString(), i.ToString()));
                ddlEndHourTour.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }
            for (var i = 0; i < 60; i++)
            {
                //var minute = i < 10 ? "0" + i : i.ToString();
                ddlBeginMinuteTour.Items.Add(new ListItem(i.ToString(), i.ToString()));
                ddlEndMinuteTour.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }
        }
        private void LoadLevel()
        {
            var ds = new DataSet();
            ds.ReadXml(MapPath("~/xml/Level.xml"));
            var dv = ds.Tables["Level"].DefaultView;
            ddlLevelTravelGuide.DataTextField = "Name";
            ddlLevelTravelGuide.DataValueField = "ID";
            ddlLevelTravelGuide.DataSource = dv;
            ddlLevelTravelGuide.DataBind();

            ddlLevelTravelGuide.Items.Insert(0, new ListItem("--- Chọn trình độ ---", "-1"));

        }
        private void LoadGender()
        {
            
            ddlGenderTravelGuide.Items.Insert(0, new ListItem("--- Chọn gioi tinh---", "-1"));
            ddlGenderTravelGuide.Items.Insert(1, new ListItem("Nam", "Nam"));
            ddlGenderTravelGuide.Items.Insert(2, new ListItem("Nữ", "Nữ"));
        }
        private void LoadCity()
        {
            ddlLanguageTravelGuide.DataSource = new LanguageController().ListLanguage();
            ddlLanguageTravelGuide.DataTextField = "LanguageName";
            ddlLanguageTravelGuide.DataValueField = "LanguageId";
            ddlLanguageTravelGuide.DataBind();

            ddlLanguageTravelGuide.Items.Insert(0, new ListItem("--- Chọn Ngon ngu ---", "-1"));
        }
        private void LoadTravelGuide()
        {
            
            ddlTravelGuideType.Items.Insert(0, new ListItem("--- Chọn loai HDV---", "-1"));
            ddlTravelGuideType.Items.Insert(1, new ListItem("Suốt tuyến", "Suốt tuyến"));
            ddlTravelGuideType.Items.Insert(2, new ListItem("Local", "Local"));
        }
    }
}